import { EquipmentEntity, EquipmentQueryParams } from "@scmt/api-lis-equipment";
import { DataPageProps, IAction, TableProps } from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { useApi } from "@scmt/core-api";
import { useEditPackage } from "./editPackage";

export const useEquipments = () => {
  const $api = useApi();

  const { editPackage } = useEditPackage();

  const queryPramas = reactive<EquipmentQueryParams>({
    pageNum: 1,
    pageSize: 20,
  });

  const { data, loadingMap, request } = useRequest(
    {
      tableData: async (q: any) => {
        const params = Object.assign({}, queryPramas, q);

        return $api.lis.equipment.query(params).then((res) => {
          const { items, pagination } = res;
          Object.assign(queryPramas, pagination);

          currentEquipment.value =
            (currentEquipment.value
              ? items.find((i) => i.id === currentEquipment.value?.id)
              : items[0]) || null;

          return res;
        });
      },
    },
    {
      tableData: {
        items: [] as EquipmentEntity[],
        pagination: {
          total: 0,
          pageNum: queryPramas.pageNum,
          pageSize: queryPramas.pageSize,
        },
      },
    }
  );

  const { tableData } = toRefs(data);
  const currentEquipment = ref<null | EquipmentEntity>(null);

  const pageProps = computed<DataPageProps>(() => {
    const queryFields: DataPageProps["queryFields"] = [
      {
        prop: "deviceName",
        label: "仪器名称",
      },
      {
        prop: "deviceCode",
        label: "仪器编码",
      },
    ];
    const dataPageProps: DataPageProps = {
      loading: loadingMap.tableData,
      queryModel: queryPramas,
      queryFields,
      onAct: onPageActions,
      onQuery: getEquipments,
      columns: 3,
    };

    return dataPageProps;
  });

  const tableProps = computed(() => {
    const tableProps: TableProps = {
      loading: loadingMap.tableData,
      data: tableData.value?.items,
      pagination: tableData.value?.pagination,
      columns: [
        {
          type: "index",
          label: "序号",
          width: "60px",
        },
        {
          prop: "deviceCode",
          label: "仪器编码",
        },
        {
          prop: "deviceName",
          label: "仪器名称",
        },
        {
          label: "操作",
          actions: [
            {
              label: "添加质控包",
              event: "add",
              type: "primary",
            },
          ],
        },
      ],
      onAct: onPageActions,
      onPaginate: getEquipments,
      onRowClick: (row) => {
        currentEquipment.value = row;
      },
      currentRow: currentEquipment.value,
      highlightCurrentRow: true,
    };
    return tableProps;
  });

  const getEquipments = (q?: any) => {
    request("tableData", q);
  };

  const onPageActions = (action: IAction, model?: any) => {
    switch (action.event) {
      case "add":
        editPackage();
        break;
    }
  };

  return {
    pageProps,
    tableProps,
    getEquipments,
    currentEquipment,
  };
};
